<template>
  <div class="anime">
    <div class="tips">点击方块触发动画</div>
    <div class="content">
      <div class="selector">
        <div class="title">单个元素</div>
        <div class="css-selector-demo">
          <div class="el" @click="addAnime($event)"></div>
        </div>
      </div>
      <div class="selector">
        <div class="title">多个元素</div>
        <div class="css-selectorall-demo" @click="addAnimeAll">
          <div class="line"></div>
          <div class="el"></div>
          <div class="line"></div>
          <div class="el"></div>
          <div class="line"></div>
          <div class="el"></div>
        </div>
      </div>
      <div class="selector">
        <div class="title">Object</div>
        <div class="css-selectorall-demo">
          <div id="el-obj" @click="addAnimeObj($event)"></div>
        </div>
      </div>
      <div class="selector">
        <div class="title">单个元素</div>
        <div class="css-selector-demo-trap">
          <div class="el" @click="addAnimeTrap($event)"></div>
        </div>
      </div>
      <div class="selector">
        <div class="title">input</div>
        <input class="input" type="text" @blur="addAnimeInput($event)" />
      </div>
      <div class="selector">
        <div class="title">关键帧</div>
        <div class="proper"></div>
      </div>
    </div>
    <div class="more">
      <span>
        更多动画请访问
        <a target="_blank" href="https://www.animejs.cn/documentation/#cssSelector">animejs</a>
      </span>
    </div>
  </div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import AnimeAnimate from "animejs";
@Component({
  components: {},
})
export default class Anime extends Vue {
  constructor() {
    super();
  }
  public mounted() {
    this.addAnimeObj();
    this.addAnimeProper();
  }
  public addAnime(event: any) {
    event.target.style.transform = "translateX(0px)";
    AnimeAnimate({
      targets: ".css-selector-demo .el",
      translateX: 250,
    });
  }
  public addAnimeAll() {
    let elements = document.querySelectorAll(".css-selectorall-demo .el");
    elements.forEach((target: any) => {
      target.style.transform = "translateX(0px)";
    });
    AnimeAnimate({
      targets: elements,
      translateX: 270,
    });
  }
  public addAnimeObj() {
    let logEl = document.getElementById("el-obj");
    let battery = {
      charged: "0%",
      cycles: 0,
    };
    AnimeAnimate({
      targets: battery,
      charged: "100%",
      cycles: 130,
      round: 1,
      easing: "linear",
      update: () => {
        if (logEl) {
          logEl.innerHTML = JSON.stringify(battery);
        }
      },
    });
  }
  public addAnimeTrap(event: any) {
    event.target.style.transform = "translateX(0px)";
    AnimeAnimate({
      targets: ".css-selector-demo-trap .el",
      left: "240px",
      backgroundColor: "#000",
      borderRadius: ["0%", "50%"],
      easing: "easeInOutQuad",
      translateX: 250,
    });
  }
  public addAnimeInput(event: any) {
    AnimeAnimate({
      targets: event.target,
      value: [0, 1000],
      round: 1,
      color: "#000",
      easing: "easeInOutExpo",
    });
  }
  public addAnimeProper() {
    AnimeAnimate({
      targets: ".proper",
      translateX: [
        { value: 250, duration: 1000, delay: 500 },
        { value: 0, duration: 1000, delay: 500 },
      ],
      translateY: [
        { value: -40, duration: 500 },
        { value: 40, duration: 500, delay: 1000 },
      ],
      scaleX: [
        { value: 4, duration: 100, delay: 500, easing: "easeOutExpo" },
        { value: 1, duration: 900 },
        { value: 4, duration: 100, delay: 500, easing: "easeOutExpo" },
        { value: 1, duration: 900 },
      ],
      scaleY: [
        { value: [1.75, 1], duration: 500 },
        { value: 2, duration: 50, delay: 1000, easing: "easeOutExpo" },
        { value: 1, duration: 450 },
        { value: 1, duration: 450 },
      ],
      easing: "easeOutElastic(1, .8)",
      loop: true,
    });
  }
}
</script>
<style lang="less" scoped>
.el {
  width: 40px;
  height: 40px;
  background: #f00;
}
.line {
  height: 2px;
}
.selector {
  padding: 10px;
  width: 600px;
  height: 400px;
  border: 1px solid #f5f5f5;
  margin: 10px;
}
.title {
  padding: 20px;
  font-size: 14px;
  color: #000;
  font-weight: bold;
  margin-bottom: 40px;
}
.anime {
  .content {
    display: flex;
    justify-content: flex-start;
    align-content: center;
    flex-wrap: wrap;
  }
}
.proper {
  width: 40px;
  height: 40px;
  background: #f00;
  border-radius: 50%;
}
</style>